package com.liuya.safe.system.login;

import com.liuya.base.BaseController;
import com.liuya.common.I18N;
import com.liuya.common.StringUtil;
import com.liuya.common.TypeParseUtil;
import com.liuya.common.WebUtil;
import com.liuya.page.ResponseData;
import com.liuya.safe.client.pojo.UserType;
import com.liuya.safe.model.SafeRole;
import com.liuya.safe.model.SafeUser;
import com.liuya.safe.system.role.service.RoleManagerService;
import com.liuya.safe.user.service.UserManagerService;
import com.liuya.safe.user.service.UserRoleManagerService;
import com.liuya.safe.util.AuthUtil;
import com.sun.org.apache.xml.internal.security.utils.I18n;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;

/**
 * 用户管理
 * @author liuya
 * @date 20160826
 */
@Controller
@Api(value = "用户登录", description = "用户登录")
public class LoginContrller extends BaseController{

    @Autowired
    @Qualifier("userManagerService")
    UserManagerService userService;
    @Autowired
    @Qualifier("userRoleManagerService")
    UserRoleManagerService userRoleService;
    @Autowired
    @Qualifier("roleManagerService")
    RoleManagerService roleMng;

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", name="get_login", method=RequestMethod.GET)
    public ModelAndView login(HttpServletRequest req, HttpServletResponse rep, ModelMap map) {
        //获取当前的Subject
        Subject currentUser = SecurityUtils.getSubject();

        if(!currentUser.isAuthenticated()){
            return request("login.html");
        }
        return request("redirect:/main.html");
    }

    @ApiOperation(value = "用户登录")
    @RequestMapping(value = "/login", name="post_login", method= RequestMethod.POST)
    public ModelAndView login(HttpServletRequest req, HttpServletResponse rep, SafeUser user) {
        String page = "main";
        //获取当前的Subject
        Subject currentUser = SecurityUtils.getSubject();
        if(!currentUser.isAuthenticated()){
            page = "login";
        }
        return request(page);
    }

    @ApiOperation(value = "注销")
//    @RequiresPermissions("sys.logout")
    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest req, HttpServletResponse rep, ModelMap map) {
        //获取当前的Subject
        Subject currentUser = SecurityUtils.getSubject();
        if(!currentUser.isAuthenticated()){
            currentUser.logout();
            return "login";
        }
        return "main";
    }

    /**
     * 验证密码是否有效
     * @param request
     * @return
     */
    @RequestMapping(value="/checkpassword")
    @ResponseBody
    public ModelAndView checkpassword(HttpServletRequest request, @Valid SafeUser user){
        ModelAndView view = request(super.createResponse("common/success.html")) ;
        SafeUser olduser = userService.selectBy(user);
        if(olduser != null){
            Date updateDate = olduser.getPassupdatetime();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH,-6);
            if(updateDate==null||(calendar.getTimeInMillis()>=updateDate.getTime())){//过期
                view.addObject("data", "no");
            }else{
                view.addObject("data", "yes");
            }

        }else{
            view.addObject("data", "username or password is wrong!");
        }
        return view ;
    }
}
